﻿using System.Windows.Forms;
using Microsoft.Win32;
using System.IO;

namespace OrbClientCacher
{
	public partial class Settings : Form
	{
		private RegistryKey key;
		
		public Settings()
		{
			InitializeComponent();
			InitializeValues();
		}

		private void InitializeValues()
		{
			this.key = Registry.CurrentUser.CreateSubKey(@"SOFTWARE\ORBIS AG\OrbClientCache");

			// CacheingTimer aus Registry lesen
			this.textboxCacheTimer.Text = string.Format("{0:D}", Utilities.GetSetValue<int>(this.key, "CachingTimer", 10));

			// Timer für den Host-Alive-Check laden
			this.textboxHostTimer.Text = string.Format("{0:D}", Utilities.GetSetValue<int>(this.key, "HostCheckTimer", 10));

			// Thread Count laden
			this.textboxThreads.Text = string.Format("{0:D}", Utilities.GetSetValue<int>(this.key, "Threads", 10));

			this.checkboxTracing.Checked = Utilities.GetSetValue<bool>(this.key, "Trace", true);

			this.textboxTracingPath.Text = Utilities.GetSetValue<string>(this.key, "TracePath", null);

			this.checkboxSmartCache.Checked = Utilities.GetSetValue<bool>(this.key, "SmartCache", true);
		}

		private void SaveSettings()
		{
			if (this.key == null) return;

			Utilities.SetValue<bool>(this.key, "Trace", this.checkboxTracing.Checked);
			Utilities.SetValue<string>(this.key, "TracePath", this.textboxTracingPath.Text);
			Utilities.SetValue<int>(this.key, "Threads", int.Parse(this.textboxThreads.Text));
			Utilities.SetValue<int>(this.key, "HostCheckTimer", int.Parse(this.textboxHostTimer.Text));
			Utilities.SetValue<int>(this.key, "CachingTimer", int.Parse(this.textboxCacheTimer.Text));
			Utilities.SetValue<bool>(this.key, "SmartCache", this.checkboxSmartCache.Checked);
		}
		
		private void onlyNumbers_KeyDown(object sender, KeyEventArgs e)
		{
			//Allow navigation keyboard arrows
			switch (e.KeyCode)
			{
				case Keys.Up:
				case Keys.Down:
				case Keys.Left:
				case Keys.Right:
				case Keys.PageUp:
				case Keys.PageDown:
					e.SuppressKeyPress = false;
					return;
				default:
					break;
			}

			//Block non-number characters
			char currentKey = (char)e.KeyCode;
			bool modifier = e.Control || e.Alt || e.Shift;
			bool nonNumber = char.IsLetter(currentKey) ||
							 char.IsSymbol(currentKey) ||
							 char.IsWhiteSpace(currentKey) ||
							 char.IsPunctuation(currentKey);

			if (!modifier && nonNumber)
				e.SuppressKeyPress = true;

			//Handle pasted Text
			if (e.Control && e.KeyCode == Keys.V)
			{
				//Preview paste data (removing non-number characters)
				string pasteText = Clipboard.GetText();
				string strippedText = "";
				for (int i = 0; i < pasteText.Length; i++)
				{
					if (char.IsDigit(pasteText[i]))
						strippedText += pasteText[i].ToString();
				}

				if (strippedText != pasteText)
				{
					//There were non-numbers in the pasted text
					e.SuppressKeyPress = true;

					//OPTIONAL: Manually insert text stripped of non-numbers
					TextBox me = (TextBox)sender;
					int start = me.SelectionStart;
					string newTxt = me.Text;
					newTxt = newTxt.Remove(me.SelectionStart, me.SelectionLength); //remove highlighted text
					newTxt = newTxt.Insert(me.SelectionStart, strippedText); //paste
					me.Text = newTxt;
					me.SelectionStart = start + strippedText.Length;
				}
				else
					e.SuppressKeyPress = false;
			}
		}

		private void buttonClose_Click(object sender, System.EventArgs e)
		{
			this.Close();
		}

		private void buttonSave_Click(object sender, System.EventArgs e)
		{
			this.SaveSettings();
			this.Close();
		}

		private void buttonTracingPath_Click(object sender, System.EventArgs e)
		{
			SaveFileDialog sfd = new SaveFileDialog();
			sfd.FileName = "OrbClientCacher.log";
			sfd.Filter = "Log files|*.log|All Files|*.*";
			sfd.InitialDirectory = Path.GetDirectoryName(this.textboxTracingPath.Text);
			
			DialogResult result = sfd.ShowDialog();
			if (result == DialogResult.OK)
			{
				this.textboxTracingPath.Text = sfd.FileName;
			}
		}
	}
}
